package com.yimu.lib.util;


import java.io.IOException;
import java.security.SecureRandom;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;

/**
 * 描述： DESUtil
 * 创建者：  Churck
 * 创建时间：16/6/8
 * 修改者： Churck
 * 修改时间：16/6/8
 * 版 本：v1.0
 */
public class DESUtil {
    private final static String DES = "DES";
    public static String encrypt(String data, String key) throws Exception {
        byte[] bt = encrypt(data.getBytes(), key.getBytes());
        String strs = BASE64.encode(bt);
        return strs;
    }
    public static String decrypt(String data, String key) throws IOException,
            Exception {
        if (data == null)
            return null;       
        byte[] buf = BASE64.decode(data);
        byte[] bt = decrypt(buf,key.getBytes());
        return new String(bt);
    }
    private static byte[] encrypt(byte[] data, byte[] key) throws Exception {

        SecureRandom sr = new SecureRandom();

        DESKeySpec dks = new DESKeySpec(key);

        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
        SecretKey securekey = keyFactory.generateSecret(dks);
 
        Cipher cipher = Cipher.getInstance(DES);

        cipher.init(Cipher.ENCRYPT_MODE, securekey, sr);
 
        return cipher.doFinal(data);
    }
    private static byte[] decrypt(byte[] data, byte[] key) throws Exception {
        SecureRandom sr = new SecureRandom();
 
        DESKeySpec dks = new DESKeySpec(key);
 
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
        SecretKey securekey = keyFactory.generateSecret(dks);
 
        Cipher cipher = Cipher.getInstance(DES);
 
        cipher.init(Cipher.DECRYPT_MODE, securekey, sr);
 
        return cipher.doFinal(data);
    }
}
